package com.test28_类库应用;

import java.util.Locale;
import java.util.ResourceBundle;

interface IMessage {
    /** 根据key获取内容 */
    public String get(String key);
}

/**
 * 实现资源加载
 */
class ResourceMessage implements IMessage {
    private static final String BASE_NAME = "com.test20_类库应用.resource.Message";
    private ResourceBundle resourceBundle;

    public ResourceMessage(Locale locale) {
        resourceBundle = ResourceBundle.getBundle(BASE_NAME, locale);
    }

    @Override
    public String get(String key) {
        try {   // 如果key不存在则抛出异常
            return this.resourceBundle.getString(key);
        } catch (Exception e) {
            return null;
        }
    }
}

class ParameterException extends RuntimeException {
    public ParameterException() {
    }

    public ParameterException(String msg) {
        super(msg);
    }
}

class MessageFactory {
    private MessageFactory() {
    }

    public static IMessage getInstance(String[] args) {
        if (args.length != 1 || !args[0].matches("\\d")) {
            throw new ParameterException("");
        }
        int select = Integer.parseInt(args[0]); // 接收初始化选项
        switch (select) {
            case 1:
                return new ResourceMessage(Locale.CHINA);
            case 2:
                return new ResourceMessage(Locale.US);
            default:
                return null;
        }
    }
}

/**
 * 国际化应用
 */
public class YootkDemo_06 {

    public static void main(String[] args) {
        IMessage message = MessageFactory.getInstance(args);
        System.out.println(message.get("welcome.info"));
    }
}
